package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @GetMapping("/page")
    public ResultInfo findByPage(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                  String name){
        //调用service查询
        Page<Setmeal> page = setmealService.findByPage(pageNum, pageSize, name);
        //返回结果
        return ResultInfo.success(page);
    }

    @PostMapping
    @CachePut(value = "setmeal",key = "#setmeal.categoryId")
    public ResultInfo save(@RequestBody Setmeal setmeal){
        //调用service查询
        setmealService.save(setmeal);
        //返回结果
        return  ResultInfo.success(null);
    }
    @DeleteMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public ResultInfo delete(Long[] ids){
        if (ids != null && ids.length>0) {
            setmealService.deleteById(ids);
        }
        return ResultInfo.success(null);
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultInfo updateFindById(@PathVariable Long id){
        //调用service根据套餐id查询
        Setmeal setmeal = setmealService.updateFindById(id);
        //返回
        return ResultInfo.success(setmeal);
    }

    @PutMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public ResultInfo update(@RequestBody Setmeal setmeal){
        //调用service进行修改
        setmealService.update(setmeal);
        //返回
        return  ResultInfo.success(null);
    }


    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmeal",allEntries = true)
    public ResultInfo updateStaus( @RequestParam Long[] ids,@PathVariable Integer status){
        //调用service进行修改状态
        setmealService.updateStatus(ids,status);
        //返回
        return  ResultInfo.success(null);
    }
}
